var express = require('express');
var router = express.Router();
const query = require('../models/index');

module.exports = {
    get:function (req,res){
        // console.log('接收到用户请求：',req.query);
        let sql = 'SELECT * FROM admin_users WHERE is_del = 1 ';
        query(sql,null)
        .then(function(data){
            // console.log('渲染时查询的数据',data)
            res.json({
                code:'200',
                msg:'查询成功',
                data:data
            })
        })
        .catch(function(err){
            console.log('渲染失败的原因',err)
            res.json({
                code:'1003',
                msg:'查询失败'
            })
        })
    },

    del:function(req,res){
        // console.log('接收到用户删除请求请求：',req.query.user_id)
        let delId = req.query.user_id;
        const sql = 'UPDATE admin_users SET is_del = 0 WHERE user_id=?';
        query(sql,delId)
        .then(function(data){
        console.log(req.query);
        res.json({
            code:'200',
            data:data,
            msg:'删除请求成功'
        })
        })
        .catch(function(err){
            console.log('删除失败的原因',err)
            res.json({
                code:'1003',
                msg:'删除失败'
            })
        })
    },

    add:function(req,res){
        console.log('这是新增数据:',req.query);
        let  add = req.query;
        let sql = 'INSERT INTO admin_users (user_name,user_pass,user_nickname,user_email,user_phone)  VALUES(?, ?,?,?,?);';
        // user_phone,user_phone,
        query(sql,[add.userName,add.userPass,add.userNickname,add.userEmail,add.userPhone])
        .then(function(data){
            res.json({
                code:'200',
                data:data,
                msg:'新增请求成功'
            })
        })
        .catch(function(err){
            console.log('新增失败的原因',err)
            res.json({
                code:'1004',
                msg:'新增失败'
            })
        })

    },
   jinZhi:function(req,res){
        console.log('这是权限数据:',req.query);
        let  add = req.query.loginData;
        let  id = req.query.id;
        let  sql =  'UPDATE quthority SET is_loginPermission = ? WHERE quthority_id=(SELECT quthority_id FROM admin_users WHERE user_id = ?);';

        query(sql,[add,id])
        .then(function(data){
            res.json({
                code:'200',
                data:data,
                msg:'权限修改成功'
            })
        })
        .catch(function(err){
            console.log('权限修改失败的原因',err)
            res.json({
                code:'1005',
                msg:'权限修改失败'
            })
        })

    },


    bianJi:function(req,res){
        console.log('这是编辑数据:',req.query);
        let sql = ' SELECT * FROM admin_users WHERE user_id = ? ';
        let  id = req.query.id;
        query(sql,[id])
        .then(function(data){
            res.json({
                code:'200',
                data:data,
                msg:'编辑查询成功'
            })
        })
        .catch(function(err){
            console.log('编辑失败的原因',err)
            res.json({
                code:'1006',
                msg:'编辑失败'
            })
        })

    }, 
    xiuGai:function(req,res){
        let id = req.query.id
        let add = req.query. bianJiInfo
        let sql = 'UPDATE admin_users SET user_name = ?, user_pass = ?, user_nickname = ?,user_email = ?,user_phone = ? WHERE user_id=?';
        // let sql = 'INSERT INTO admin_users (user_name,user_pass,user_nickname,user_email,user_phone)  VALUES(?, ?,?,?,?);';
        // query(sql,[add.userName,add.userPass,add.userNickname,add.userEmail,add.userPhone])
        console.log('修改时的数据2',req.query);
        console.log('修改时的数据2',id);
        
        query(sql,[add.userName,add.userPass,add.userNickname,add.userEmail,add.userPhone,id])
        .then(function(data){
            res.json({
                code:'200 ',
                msg:'编辑修该成功',
                data:data
             })
        })
        .catch(function(err){
            res.json({
               code:'1007 ',
               msg:'编辑修该失败'
            })
        })


    }
};

